package com.xing.fruit.dao.user;

import com.xing.fruit.dao.DatabaseDao;
import com.xing.fruit.pojo.User;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class UserDaoImpl implements UserDao{
    DatabaseDao databaseDao = new DatabaseDao();
    //得到登录用户
    @Override
    public User getLoginUser(String username, String password) {
        String sql = "";
        User user = null;
        ResultSet resultSet = null;
        //邮箱格式匹配
        String expr = "^([a-zA-Z0-9_\\-\\.]+)@((\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.)|(([a-zA-Z0-9\\-]+\\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})$";
        //手机号码格式匹配
        Pattern pattern = Pattern.compile("^(13[0-9]|15[0-9]|18[0-9]|19[0-9])\\d{8}$");
        Matcher matcher = pattern.matcher(username);
        //判断是否是邮箱
        if (username.matches(expr)) {
            sql = "select * from user where password = ? and email = ?";

        } else if (matcher.matches()) {  //判断是否是手机号
            sql = "select * from user where password = ? and phone = ?";
        } else {  //为用户名
            sql = "select * from user where password = ? and username = ?";
       }

        Object[] objects = {password, username};
        try {
            resultSet = databaseDao.execute(sql, objects);
            while (resultSet.next()){
                user = new User();
                user.setUid(resultSet.getInt("uid"));
                user.setNickname(resultSet.getString("nickname"));
                user.setUsername(resultSet.getString("username"));
                user.setPassword(resultSet.getString("password"));
                user.setPhone(resultSet.getString("phone"));
                user.setAge(resultSet.getInt("age"));
                user.setSex(resultSet.getString("sex"));
                user.setEmail(resultSet.getString("email"));
                user.setAddress(resultSet.getString("address"));
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }finally {
            databaseDao.closeAll();
        }
        return user;
    }
    //添加用户
    @Override
    public int userReg(User user) {
        String sql = "insert user (uid,nickname,username,password,sex,age,phone,email,address)values(null,?,?,?,?,?,?,?,?)";
        Object[] objects = {user.getNickname(),user.getUsername(),user.getPassword(),user.getSex(),user.getAge(),
        user.getPhone(),user.getEmail(),user.getAddress()};
        int update = 0 ;
        try {
             update = databaseDao.update(sql, objects);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }finally {
            databaseDao.closeAll();
        }
        return update;
    }
    //得到全部用户
    @Override
    public List<User> getUserList() {
        String sql = "select * from user";
        Object[] objects = null;
        List<User> list = new ArrayList<User>();
        try {
            ResultSet resultSet = databaseDao.execute(sql, objects);
            while (resultSet.next()){
                User user = new User();
                user.setUid(resultSet.getInt("uid"));
                user.setNickname(resultSet.getString("nickname"));
                user.setUsername(resultSet.getString("username"));
                user.setPassword(resultSet.getString("password"));
                user.setSex(resultSet.getString("sex"));
                user.setAge(resultSet.getInt("age"));
                user.setPhone(resultSet.getString("phone"));
                user.setEmail(resultSet.getString("email"));
                user.setAddress(resultSet.getString("address"));
                list.add(user);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }finally {
            databaseDao.closeAll();
        }
        return list;
    }
    //通过id查询用户
    @Override
    public User getUserById(int id) {
        String sql = "select * from user where uid = ?";
        Object[] objects = {id};
        ResultSet resultSet = null;
        User user = null;
        try {
            resultSet = databaseDao.execute(sql, objects);
            while (resultSet.next()){
                user = new User();
                user.setUid(resultSet.getInt("uid"));
                user.setNickname(resultSet.getString("nickname"));
                user.setUsername(resultSet.getString("username"));
                user.setPassword(resultSet.getString("password"));
                user.setPhone(resultSet.getString("phone"));
                user.setAge(resultSet.getInt("age"));
                user.setSex(resultSet.getString("sex"));
                user.setEmail(resultSet.getString("email"));
                user.setAddress(resultSet.getString("address"));
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }finally {
            databaseDao.closeAll();
        }
        return user;
    }
    //修改用户
    @Override
    public int updateUser(User user) {
        String sql ="update user set nickname = ?,username = ?,sex = ?,age = ?,phone = ?,email = ? where uid = ?";
        Object[] objects = {user.getNickname(),user.getUsername(),user.getSex(),user.getAge(),user.getPhone(),user.getEmail(),user.getUid()};
        int update = 0;
        try {
            update = databaseDao.update(sql, objects);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }finally {
            databaseDao.closeAll();
        }
        return update;
    }

    //修改密码
    @Override
    public int updatePwd(String password,String username) {
        String sql = "update user set password = ? where username = ?";
        Object[] objects = {password, username};
        int result = 0;
        try {
            result = databaseDao.update(sql, objects);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } finally {
            databaseDao.closeAll();
        }
        return result;
    }

    //删除用户
    @Override
    public int deleteUser(int id) {
        String sql = "delete from user where uid = ?";
        Object[] objects = {id};
        int update = 0 ;
        try {
            update = databaseDao.update(sql, objects);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }finally {
            databaseDao.closeAll();
        }
        return update;
    }
}
